package com.qike.duola.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.qike.duola.domain.DuolaProject;
import com.qike.duola.dto.*;
import com.qike.duola.response.BaseResult;
import com.qike.duola.response.RetResponse;
import com.qike.duola.service.DuolaCustomerService;
import com.qike.duola.service.DuolaProjectService;
import com.qike.duola.service.IsysResourceService;
import com.qike.duola.service.IsysUserService;
import com.qike.duola.utils.TokenUtil;
import com.qike.duola.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(description = "工程控制器")
@CrossOrigin
public class ProjectController {

    private static Logger logger = LoggerFactory.getLogger(ProjectController.class);

    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private DefaultKaptcha captchaProducer;
    @Autowired
    private DuolaProjectService duolaProjectService;
    @Autowired
    private IsysUserService userService;
    @Autowired
    private IsysResourceService isysResourceService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 添加工程
     *
     * @return
     */
    @PostMapping("/projectAdd")
    @ApiOperation("添加工程")
    @ResponseBody
    public BaseResult projectAdd(@RequestBody ProjectAddDto projectAddDto) {
        try {
            duolaProjectService.projectAdd(projectAddDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 编辑工程
     *
     * @return
     */
    @PostMapping("/projectEdit")
    @ApiOperation("编辑工程")
    @ResponseBody
    public BaseResult projectEdit(@RequestBody ProjectEditDto projectEditDto) {
        try {
            duolaProjectService.projectEdit(projectEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 工程列表
     *
     * @return
     */
    @GetMapping("/projectList")
    @ApiOperation("工程列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = ProjectListVo.class)
    })
    public BaseResult projectList(ProjectListDto projectListDto) {
        try {
            ProjectListVo vo = duolaProjectService.projectList(projectListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 工程名称下拉
     *
     * @return
     */
    @GetMapping("/projectPullList")
    @ApiOperation("工程名称下拉")
    @ResponseBody
    @Cacheable(value = {"projectPullList"})
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = EnumList.class)
    })
    public BaseResult projectPullList(String name) {
        try {
            List<EnumList> vo = duolaProjectService.projectPullList(name);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 退料转料工程下拉
     *
     * @return
     */
    @GetMapping("/ReTurnProjectPullList")
    @ApiOperation("退料转料工程下拉")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = EnumList.class)
    })
    public BaseResult ReTurnProjectPullList(String name,Integer transId) {
        try {
            List<EnumList> vo = duolaProjectService.ReTurnProjectPullList(name,transId);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 通过客户id获取已审批的工程列表
     *
     * @return
     */
    @GetMapping("/projectPullListByCustomerId")
    @ApiOperation("通过客户id获取已审批的工程列表")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = ProjectPullListByCustomerIdVo.class)
    })
    public BaseResult projectPullListByCustomerId(String name,Integer customerId) {
        try {
            List<ProjectPullListByCustomerIdVo> vo = duolaProjectService.projectPullListByCustomerId(name,customerId);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 工程详情
     *
     * @return
     */
    @GetMapping("/projectInfo")
    @ApiOperation("工程详情")
    @ResponseBody
    public BaseResult projectInfo(Integer id) {
        try {
            ProjectInfoVo vo = duolaProjectService.projectInfo(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 删除工程
     *
     * @return
     */
    @PostMapping("/projectDel")
    @ApiOperation("删除工程")
    @ResponseBody
    public BaseResult projectDel(Integer id) {
        try {
            duolaProjectService.projectDel(id);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


}
